package com.handmark.pulltorefresh.library.internal;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Orientation;
import com.handmark.pulltorefresh.library.R;

/**
 * @author hailong.han 下午2:38:57
 */
public class JumpLoadingLayout extends LoadingLayout {

    // private final Matrix mHeaderImageMatrix;
    private GifAnimation mGifAnimation;

    private int[] mGifRes = {R.drawable.dropdown_loading_00,
            R.drawable.dropdown_loading_01, R.drawable.dropdown_loading_02,
            R.drawable.dropdown_loading_03};

    /**
     * @param context
     * @param mode
     * @param scrollDirection
     * @param attrs
     */
    public JumpLoadingLayout(Context context, Mode mode,
                             Orientation scrollDirection, TypedArray attrs) {
        super(context, mode, scrollDirection, attrs);

    }

    @Override
    protected int getDefaultDrawableResId() {
        return R.drawable.default_ptr_rotate;
    }

    @Override
    protected void onLoadingDrawableSet(Drawable imageDrawable) {

    }

    int mPrevIndex = -1;

    @Override
    protected void onPullImpl(float scaleOfLayout) {

        int index = (int) (scaleOfLayout / 1f * 10);
        if (index == mPrevIndex) {
            return;
        } else {
            if (index > 15) {
                index = 15;
            }

            int res = getResources().getIdentifier(
                    String.format("dropdown_anim_%02d", index), "drawable",
                    getContext().getPackageName());
            mHeaderImage.setImageResource(res);
            mPrevIndex = index;
        }

    }

    @Override
    protected void pullToRefreshImpl() {

    }

    @Override
    protected void refreshingImpl() {
        if (mGifAnimation == null) {
            mGifAnimation = new GifAnimation(mHeaderImage, mGifRes);
        }
        mGifAnimation.start();
    }

    @Override
    protected void releaseToRefreshImpl() {

    }

    @Override
    protected void resetImpl() {
        mHeaderImage.clearAnimation();
        if (mGifAnimation != null) {
            mGifAnimation.stop();
        }
    }

}
